package com.xinli.xinlifm.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.ForumBean;
import com.xinli.xinlifm.http.bean.FroumDetailCommentBean;
import com.xinli.xinlifm.mvp.iview.MvpView;
import com.xinli.xinlifm.mvp.model.SuperModel;
import com.xinli.xinlifm.view.fragment.forum.FroumFragmentAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

public class SecondActivity extends AppCompatActivity implements MvpView<FroumDetailCommentBean>, View.OnClickListener {
    private RecyclerView recyclerView;
    private FroumFragmentAdapter froumFragmentAdapter;
    private List<Object> list = new ArrayList<>();
    private SuperModel model = new SuperModel();
    private int limit = 10;
    private int offset = 0;
    private int allComment;
    private int beanId;
    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        Object obj = bundle.getSerializable("bean");
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.forum_comment).setOnClickListener(this);
        findViewById(R.id.forum_share).setOnClickListener(this);
        Log.e("s", "类名==SecondActivity" + "方法名==onCreate=====:" + obj);
        if(bundle !=null){
            ForumBean.DataBean bean = (ForumBean.DataBean) bundle.getSerializable("bean");
            Log.e("s", "类名==DetailFragment" + "方法名==onCreateView===dcc==:" + bean);
            recyclerView = ((RecyclerView) findViewById(R.id.detail_recyclerView));
            linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.addOnScrollListener(new OnRecyclerScrollListener());
            froumFragmentAdapter = new FroumFragmentAdapter(this,list);
            recyclerView.setAdapter(froumFragmentAdapter);
            froumFragmentAdapter.setType(1);
            list.add(0,bean);
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
            Log.e("自定义标签", "类名==DetailFragment" + "方法名==onCreateView=====:" + bean.getId());
            allComment = Integer.parseInt(bean.getCommentnum());
            beanId = Integer.parseInt(bean.getId());
            model.getDetailCommentData(beanId, offset , limit);
        }
    }
    @Subscribe
    @Override
    public void showView(FroumDetailCommentBean data) {
       // list.addAll(data.getData());
        froumFragmentAdapter.addAll(data.getData());
        Log.e("e", "类名==DetailFragment" + "方法名==showView=====:" + data);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.forum_comment:
                Intent intent = new Intent(this,CommentActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.in_anim,R.anim.out_anim);
                finish();
                break;
            case R.id.forum_share:

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                View view1 = LayoutInflater.from(this).inflate(R.layout.forum_share_dialog,null);
                builder.setView(view1);
                AlertDialog dialog = builder.create();
                Window window = dialog.getWindow();
                window.setGravity(Gravity.BOTTOM);
                dialog.show();
                break;
        }
    }

    public class OnRecyclerScrollListener extends RecyclerView.OnScrollListener {

        int lastVisibleItem = 0;

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            Log.e("自定义标签", "类名==OnRecyclerScrollListener" + "方法名==onScrollStateChanged===crr==:" + newState);
            if (froumFragmentAdapter != null && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 ==

                    froumFragmentAdapter.getItemCount()) {
                //滚动到底部了，可以进行数据加载等操作
                Log.e("自定义标签", "类名==OnRecyclerScrollListener" + "方法名==onScrollStateChanged=====:" + "hahahaha");
                if (offset >= allComment) {
                    froumFragmentAdapter.setLastShow(2);
                    return;
                }
                offset = offset + limit;
                model.getDetailCommentData(beanId, offset , limit);
            }
        }
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
        }
    }
}
